﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Demo.BusinessLogic.Interface;
using Demo.BusinessLogic.Concrete;
using Demo.Common.ExceptionHandlers;

namespace Demo.Web
{
    public partial class TwoColumn : System.Web.UI.Page
    {
        private readonly ISampleData _sampleData;

        public TwoColumn(): this(new SampleData()) { }

        public TwoColumn(ISampleData sampleData)
        {
            _sampleData = sampleData;
        }
       

        protected void Page_Load(object sender, EventArgs e)
        {

        }
        //Exception Raised by Database Management System such as Primary Key violated or Object does not exist etc.
        //System should log the thrown exception in a sink (Text File). Replace the Exception with a User friendly Message and propagate it to UI 
        //passing through the Business Logic Layer. 
        protected void getCust_Click(object sender, EventArgs e)
        {
            try
            {
                DataTable dataTable = _sampleData.GetUserList();
            }
            catch (Exception exception)
            {
                lblError.Text = exception.Message;
            }
        }


        //While processing the request an error occurred in UI. For example, Null reference exception etc. 
        //System should log the thrown exception in a sink (Text File). Replace the Exception with a User friendly Message and should propagate that to UI. 
        protected void uIError_Click(object sender, EventArgs e)
        {
            try
            {
                //intentially raising Divide by Zero exception
                int a, b, c;
                a = 20;
                b = 0;
                c = a / b;
            }
            catch (Exception exception)
            {
                UserInterfaceExceptionHandler.HandleExcetion(ref exception);
                lblError.Text = exception.Message;
            }
        }

        //While processing the request an error occurred in Business Logic such as Divide-by-zero, Object not set, Null reference exception etc.
        //System should log the thrown exception in a sink (Text File). Replace the Exception with a User friendly Message and should propagate that to UI. 
        protected void bIError_Click(object sender, EventArgs e)
        {
            try
            {
                int result = _sampleData.SomeCalculation(10, 0);
            }
            catch (Exception exception)
            {
                lblError.Text = exception.Message;
            }
        }
        

        //Based upon some certain validation check / business rule a Custom Error is raised from Stored Procedure. 
        //Thrown Custom Error message should be propagated to UI without Logging assuming these error messages 
        //will be some kind of alternative flows instead of real exception. 
        protected void custErrorDA_Click(object sender, EventArgs e)
        {
            try
            {
                DataTable dataTable = _sampleData.GetUserData("");
            }
            catch (Exception exception)
            {
                lblError.Text = exception.Message;
            }
        }


        //Based upon some certain business validation check, a custom exception was thrown from Business Logic Layer. 
        //Thrown Custom Error message should be propagated to UI without Logging assuming these error messages
        //will be some kind of alternative flows instead of real exception. 
        protected void custErrorBA_Click(object sender, EventArgs e)
        {
            try
            {
                _sampleData.SaveData("AY", 1500);
            }
            catch (Exception exception)
            {
                lblError.Text = exception.Message;
            }
        }
    }
}